//
//  MainTabBat.swift
//  weibo_yaoye
//
//  Created by mac on 15/5/11.
//  Copyright (c) 2015年 mac. All rights reserved.
//

import UIKit
 //*********  step (3 创建自定义bar)
class MainTabBat: UITabBar {
     //*********  step (4 重写layousubview 此方法用来重新布局安妮)
    override func layoutSubviews() {
        let btnCount = 5;
        let w = self.bounds.size.width / CGFloat(btnCount)
        let h = self.bounds.size.height
        //按钮frame的基值
        var frame = CGRectMake(0, 0, w, h)
        var index = 0
        for v in subviews as! [UIView]
        {
            if v is UIControl && !(v is UIButton)
            {
                v.frame = CGRectOffset(frame, CGFloat(index) * w, 0)
                index++
            }
            if (index == 2)
            {
                 //*********  step (5 给加号按钮留一个位子)
                index++
            }
        }
        
         //*********  step (7 加载加号按钮 MainViewController)
        self.composedButton.frame = frame
        self.composedButton.center = CGPointMake(self.center.x, h*0.5)
        

    }

   //*********  step (6 懒加载加号按钮)
    lazy var composedButton: UIButton = {
        
        let btn = UIButton()
        
        btn.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: UIControlState.Normal)
        btn.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: UIControlState.Selected)
        btn.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: UIControlState.Normal)
        btn.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: UIControlState.Selected)
        
        self.addSubview(btn)
        
        return btn
    
    }()
    
}
